package cn.edu.zust.se.service.file.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.edu.zust.se.api.common.R;
import cn.edu.zust.se.api.entity.Folder;
import cn.edu.zust.se.api.entity.bo.FolderBo;
import cn.edu.zust.se.api.entity.vo.FileAndFolderVo;
import cn.edu.zust.se.service.file.service.FileService;
import cn.edu.zust.se.service.file.service.FolderService;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.tree.Tree;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;

@RestController
@RequestMapping("/folder")
@RequiredArgsConstructor
public class FolderController {
    private final FolderService folderService;
    private  final FileService  fileService;
    /**
     * 下载文件夹
     *
     * @param folderId
     * @return
     */
    @GetMapping("/download/folderId")
    public ResponseEntity<byte[]> downloadFolder(Long folderId) {
        ResponseEntity<byte[]> folder = folderService.downloadFolder(folderId);
        return folder;

    }

    /**
     * 创建文件夹
     *
     * @param bo
     * @return
     */
    @PostMapping("")
    public R<Void> createFolder(@RequestBody FolderBo bo) {
        Boolean success = folderService.createFolder(bo);
        if (success) {
            return R.ok();
        } else {
            return R.fail("创建失败");
        }
    }

    /**
     * 获取文件夹树
     *
     * @param folderId
     * @return
     */
    @GetMapping("/tree/{folderId}")
    public R<Tree<String>> getFolderTree(@PathVariable Long folderId) {
        Tree<String> tree = folderService.getFolderTree(folderId);
        return R.ok(tree);
    }

    //根据上级文件夹id获取下级文件夹
    //种类type 1为正常文件 2为回收站文件
    @GetMapping("/getFoldersByFolderId/{folderId}/{type}")
    public List<Folder> getFoldersByFolderId(@PathVariable Long folderId, @PathVariable Integer type) {
        return folderService.getFoldersByFolderId(folderId, type);
    }

    //文件夹重命名和移动（文件夹更新）
    //若上级文件夹在最外层则给0
    @PutMapping("/updateFolder")
    public R<Void> updateFolder(@RequestBody FolderBo folderBo) {
        if (folderService.updateById(folderBo)) {
            return R.ok();
        }
        return R.fail();
    }
    //文件夹删除
    @DeleteMapping("/deleteFolder/{folderId}/{type}")
    public R<Void> deleteFolder(@PathVariable Long folderId, @PathVariable Integer type) throws IOException {
        if (folderService.delFileAndFolder(folderId,type)) {
            return R.ok("文件夹删除成功");
        }
        return R.fail("出现错误，文件夹删除失败");
    }
    // 文件夹复制
    @PostMapping("/copyFolder/{folderId}/{targetFolderId}")
    public R<Boolean> copyFolder(@PathVariable Long folderId, @PathVariable Long targetFolderId) {
        if (folderService.copyFolder(folderId, targetFolderId)) {
            return R.ok("文件夹复制成功");
        }
        return R.fail("出现错误，文件夹复制失败");
    }

    //文件夹新增
    //文件夹新增
    @PostMapping("/addFolder")
    public R<Boolean> addFolder(@RequestBody FolderBo folderBo){
        System.out.println(folderBo+"===================");
        Folder folder = BeanUtil.copyProperties(folderBo, Folder.class);
        //处于登录状态则不需要给id
        if (folder.getUserId() == null)
        {
            folder.setUserId(StpUtil.getLoginIdAsLong());
        }
        if (folderService.save(folder)) {
            return R.ok();
        }
        return R.fail();
    }
    //获取个人网盘的文件
    @GetMapping("/getFiles/{folderId}/{type}")
    public R<FileAndFolderVo> getFilesAndFolder(@PathVariable Long folderId, @PathVariable Integer type) {
        FileAndFolderVo fileAndFolderVo = new FileAndFolderVo();
        fileAndFolderVo.setFiles(fileService.getFilesByFolderId(folderId,type));
        fileAndFolderVo.setFolders(folderService.getFoldersByFolderId(folderId,type));
        if (fileAndFolderVo.getFiles().isEmpty()&&fileAndFolderVo.getFolders().isEmpty()){
            return R.fail("无法获取当前用户网盘文件",null);
        }
        return R.ok(fileAndFolderVo);
    }

    /**
     * 移动文件夹
     *
     * @param folderBo
     * @return
     */
    @PutMapping("/moveFolder")
    public R<Boolean> moveFolder(@RequestBody FolderBo folderBo) {
        if (folderService.move(folderBo)) {
            return R.ok();
        }
        return R.fail();
    }

    /**
     * 恢复文件夹
     * @param folderId
     * @return
     */
    @PutMapping("/recycle/{folderId}")
    public R<Void> recycle(@PathVariable Long folderId) {
        Boolean success = folderService.recycle(folderId);
        return success ? R.ok() : R.fail("恢复失败");
    }

}
